#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib', 't/lib/';
use Test::More 0.88;
use SubtestCompat;
use Getopt::Long qw/:config passthrough/;
use List::Util qw/first/;
use TestBridge;
use TestUtils;

#--------------------------------------------------------------------------#
# Note: This program is both the proxy to select .tml files for 'prove' and the
# test-runner that 'prove' executes.
#--------------------------------------------------------------------------#

# match path prefix under t/
my %BRIDGE_MAP = (
    'tml-local/dump-error'      => \&test_dump_error,
    'tml-local/load-error'      => \&test_load_error,
    'tml-local/load-warning'    => \&test_load_warning,
    'tml-local/perl-to-yaml'    => \&test_perl_to_yaml,
    'tml-local/yaml-roundtrip'  => \&test_yaml_roundtrip,
    'tml-spec/basic-data.tml'   => \&test_yaml_json,
    'tml-spec/unicode.tml'      => \&test_code_point,
    'tml-world'                 => \&test_yaml_roundtrip,
);

sub main {
    my ($verbose, $run_tests);
    GetOptions(
        'run_test' => \$run_tests,
    );

    if ( $run_tests ) {
        my $file = shift @ARGV;
        exit 0 unless -f $file;
        my ($bridge) = first { $file =~ m{^t/\Q$_} } keys %BRIDGE_MAP;
        die "No bridge found for $file" unless $bridge;

        run_testml_file(
            $file,
            sub {
                my ($file, $blocks) = @_;
                subtest "TestML dev runner: $file" => sub {
                    $BRIDGE_MAP{$bridge}->($_) for @$blocks;
                };
                done_testing;
            },
        );
    }
    else {
        my (@opts, @files, @patterns);
        for (@ARGV) {
            if ( /^-/ ) {
                push @opts, $_;
            }
            elsif ( -f ) {
                push @files, $_;
            }
            else {
                push @patterns, $_;
            }
        }

        # if we got no files or patterns, treat that as taking anything
        @patterns = "." if !@patterns && !@files;

        if (@patterns) {
            FILE: for my $file ( find_tml_files('t') ) {
                if ( first { $file =~ /$_/ } @patterns ) {
                        push @files, $file;
                }
            }
        }

        exec( 'prove', @opts, '--exec', "$0 --run_test", @files )
            if @files;
    }
}

main;

__END__

=head1 NAME

t/tml - run .tml files matching a pattern

=head1 SYNOPSIS

    t/tml [prove options] [patterns]

=head1 USAGE

This program runs F<prove> against a set of F<.tml> files using their
corresponding test bridge functions.

Any arguments beginning with C<-> will be passed through to F<prove>.  All
other arguments will be used as patterns to select F<.tml> files found anywhere
under the F<t> directory.  You can use shell globbing syntax, and let the shell
expand the patterns, or you can quote/escape the patterns and let them be
treated as Perl regular expressions.

For example:

    t/tml unicode       # paths matching qr/unicode/
    t/tml basic uni     # paths matching qr/basic/ or qr/uni/
    t/tml 'local.*re'   # paths matching qr/local.*re/
    t/tml '\d+'         # paths matching qr/\d+/

Examples of options for prove:

    t/tml -v quoting    # verbose run of paths matching qr/quoting/
    t/tml -j9 world     # parallel run of paths matching qr/world/
    t/tml -j9           # parallel run of all .tml files

=cut
